<template>
	<view class="contact-list">
		<view class="acea-row row-middle row-between contact-p" v-for="(item,index) in list" :key="index">
			<view class="acea-row row-middle row-between contact-r">
				<view class="add-icon" @click="deleteContact(index)">
					<image src="../static/images/sub_icon.png" mode=""></image>
				</view>
				<view class="name"
					@click="toPage('/pages/releases/addShopContact/addShopContact?info='+JSON.stringify(item) + '&infoIndex='+index)">
					{{item.username}}
				</view>
				<view class="gray-text" v-if='item.type == 1'>
					房东
				</view>
				<view class="gray-text" v-if='item.type == 2'>
					老板
				</view>
				<view class="gray-text" v-if='item.type == 3'>
					雇员
				</view>
				<view class="gray-text"
					@click="toPage('/pages/releases/addShopContact/addShopContact?info='+JSON.stringify(item) + '&infoIndex='+index)">
					{{item.phone}}
				</view>
			</view>
			<view class="acea-row row-middle check-box" v-if="item.open == 0" @click="openChange(item,index,1)">
				<image src="../static/images/checkbox_no.png" mode="" >
				</image>
				<text>公开</text>
			</view>
			<view class="acea-row row-middle check-box" v-if="item.open == 1" @click="openChange(item,index,0)">
				<image src="../static/images/checkbox_on.png" mode="" >
				</image>
				<text>公开</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		toPage
	} from '@/filters/commFilter.js'
	export default {
		props:{
			list:{
				type: Array,
				default:()=>([])
			},
			dataform:{
				type: Object,
				default: ()=>({})
			}
		},
		data() {
			return {
				
			}
		},
		mounted() {
			let that = this;
			uni.$on('contact', function(data) {
				if (that.list.findIndex(item => item.phone == data.phone) == -1) {
					// that.list.push(data)
					that.$emit('add',data,()=>{
						that.dataform.phoneExternal = (that.list[0].contactPhone || that.list[0].phone)
						that.dataform.contactExternal = (that.list[0].contactName || that.list[0] .username)
					})
				}
			})
			uni.$on('initContact', function(data) {
				if (that.list.length < 1) {
					// that.list.push(data)
					that.$emit('add',data,()=>{
						console.log("datacontactssssssss", data)
						that.dataform.phoneExternal = (that.list[0].contactPhone || that.list[0].phone)
						that.dataform.contactExternal = (that.list[0].contactName || that.list[0].username)
					})
				}
			})
			uni.$on('upContact', function(data) {
				// that.list.splice(data.index, 1, data.info)
				that.$emit('del',data.index,data.info,()=>{
					that.dataform.phoneExternal = (that.list[0].contactPhone || that.list[0].phone)
					that.dataform.contactExternal = (that.list[0].contactName || that.list[0].username)
				})
				
			})
		},
		methods: {
			toPage,
			openChange(row, index,status) {
				row.open = status;
				this.$emit('del',index,row)
				// console.log("row",this.list)
				// that.$emit('del',data.index,data.info)
			},
			//删除联系人
			deleteContact(index) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除'+this.list[index].username,
					success: function (res) {
						if (res.confirm) {
							that.$emit('del',index)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				// this.list.splice(index, 1)
				
			},
		}
	}
</script>

<style scoped>
		@import '/static/css/releaseForm.css';
		.contact-p{
			margin-bottom: 25rpx;
		}
		.contact-list .border-b{
			padding: 10rpx 0
		}
		.add-icon image{
			height: 35rpx;
			width: 35rpx;
		}
</style>